<?php
require('../include/init.inc.php');
$method = "";
extract($_REQUEST, EXTR_IF_EXISTS);

if ($method == 'upload') {
    $file = $_FILES["productupfile"];

    $max_size = "2000000"; //最大文件限制（单位：byte）
    $fname = $file['name'];

    $ftype = strtolower(substr(strrchr($fname, '.'), 1));
//文件格式
    $uploadfile = $file['tmp_name'];
    if (Common::isPost()) {
        if (is_uploaded_file($uploadfile)) {
            if ($file['size'] > $max_size) {
                OSAdmin::alert("error", "导入文件太大");
            }
            if ($ftype != 'xls' || $ftype != 'csv') {
                OSAdmin::alert("error", "文件类型错误");
            }
        } else {
            OSAdmin::alert("error", "文件为空");
        }
    }


    $file_arr = ExcelReader::readXLS($uploadfile);
    unset($file_arr[0]);


//插入数据库
    $ret = AdminProducts::replace($file_arr);

    if ($ret) {
        SysLog::addLog(UserSession::getUserName(), 'Update', 'ProductsUpload', $method, json_encode($file));
        OSAdmin::alert("success", "导入成功");
    } else {
        OSAdmin::alert("error", "数据库出错");
    }
}

Template::display('snap/products_upload.tpl');